/*
 * Copyright (c) 2025 EasyAdmin
 * All rights reserved.
 *
 * This file is part of the [EasAdmin] project.
 * Unauthorized copying of this file, via any medium, is strictly prohibited.
 * Proprietary and confidential.
 *
 * Author: EasyAdmin ( 1073602@qq.com )
 * Website: https://www.xhxiao.com
 */
package com.easy.app.controller.system;

import com.easy.framework.common.model.R;
import com.easy.framework.common.validate.Add;
import com.easy.framework.common.validate.Update;
import com.easy.framework.database.page.Pagination;
import com.easy.framework.poi.utils.ExcelUtils;
import com.easy.framework.web.base.BaseController;
import com.easy.module.log.annotation.Log;
import com.easy.module.log.enums.Operation;
import com.easy.module.system.entity.SysPost;
import com.easy.module.system.query.SysPostQuery;
import com.easy.module.system.service.ISysPostService;
import jakarta.servlet.http.HttpServletResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

/**
 * 岗位信息表 控制器
 *
 * @author EasyAdmin ( 1073602@qq.com )
 * @date 2025-08-19
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/system/post")
public class SysPostController extends BaseController {
    private final ISysPostService sysPostService;

    /**
     * 获取岗位列表
     */
    @PreAuthorize("@perm.hasPermission('system:post$list')")
    @GetMapping("/page")
    public R<Pagination<SysPost>> page(SysPostQuery query) {
        return R.success(sysPostService.page(query));
    }

    @Log(title = "岗位管理", type = Operation.EXPORT)
    @PreAuthorize("@perm.hasPermission('system:post$export')")
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysPostQuery query) throws IOException {
        List<SysPost> list = sysPostService.list(query);
        ExcelUtils.exportExcel(response, list, "岗位数据");
    }

    /**
     * 根据岗位编号获取详细信息
     */
    @PreAuthorize("@perm.hasPermission('system:post$query')")
    @GetMapping(value = "/{postId}")
    public R<SysPost> get(@PathVariable("postId") Long postId) {
        return R.success(sysPostService.getById(postId));
    }

    /**
     * 新增岗位
     */
    @PreAuthorize("@perm.hasPermission('system:post$add')")
    @Log(title = "岗位管理", type = Operation.INSERT)
    @PostMapping
    public R add(@Validated({Add.class}) @RequestBody SysPost post) {
        if (sysPostService.exists(SysPost::getPostName, post.getPostName(),SysPost::getId, post.getId())) {
            return R.fail("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (sysPostService.exists(SysPost::getPostKey, post.getPostKey(),SysPost::getId, post.getId())) {
            return R.fail("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return R.result(sysPostService.add(post));
    }

    /**
     * 修改岗位
     */
    @PreAuthorize("@perm.hasPermission('system:post$edit')")
    @Log(title = "岗位管理", type = Operation.UPDATE)
    @PutMapping
    public R edit(@Validated({Update.class}) @RequestBody SysPost post) {
        if (sysPostService.exists(SysPost::getPostName, post.getPostName(),SysPost::getId, post.getId())) {
            return R.fail("修改岗位'" + post.getPostName() + "'失败，岗位名称已存在");
        } else if (sysPostService.exists(SysPost::getPostKey, post.getPostKey(),SysPost::getId, post.getId())) {
            return R.fail("修改岗位'" + post.getPostName() + "'失败，岗位编码已存在");
        }
        return R.result(sysPostService.update(post));
    }

    /**
     * 删除岗位
     */
    @PreAuthorize("@perm.hasPermission('system:post$remove')")
    @Log(title = "岗位管理", type = Operation.DELETE)
    @DeleteMapping("/{postIds}")
    public R remove(@PathVariable("postIds") Long[] postIds) {
        return R.result(sysPostService.deleteByIds(postIds));
    }

    /**
     * 获取所有岗位
     */
    @GetMapping("/list")
    public R<List<SysPost>> list(SysPostQuery query) {
        return R.success(sysPostService.list(query));
    }
}
